package com.fz.controller;

import com.fz.dao.BookDAO;
import com.fz.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by webrx on 2017-09-13.
 */
@Controller
public class Index {

    @Resource
    protected BookDAO bdao;

    @RequestMapping
    public String i(Model m){
        m.addAttribute("book",bdao.queryAll());
       return "indexx";
    }

    @RequestMapping("/insert")
    public String zeng(){
        return "addbook";
    }

    //以下两种添加方式都可以
    /*@RequestMapping(value = "/savebook",method = RequestMethod.POST)
    public String savebook(String name,double price){
        Book bb=new Book();
        bb.setName(name);
        bb.setPrice(price);
        bb.setBdate(new Date());
        bdao.add(bb);
        return "redirect:/";
    }*/

    @RequestMapping(value = "/savebook",method = RequestMethod.POST)
    public String savebook(String name,double price,String bdate){
        bdao.save(name, price, bdate);
        return "redirect:/";
    }

    //删除的方法
    @RequestMapping("delete")
    public String del(int id){
        bdao.delete(id);
        return "redirect:/";   //由于本例显示的控制器直接默认的，所以直接找根就好，如果要跳转到指定的控制器，示例：redirect:/savebook
    }
}
